﻿//
//公用Ajax请求对象
//
var Ajax = {
    LoadContent: function (url, onload, onerror, sendData, dataType, requestType, callBackGuid) {
        this.url = url;
        this.req = null;
        this.data;
        this.onload = onload;
        this.onerror = onerror ? onerror : this.defaultError;
        this.sendData = sendData;
        this.dataType = dataType || "xml";
        this.requestType = requestType;
        this.callBackGuid = callBackGuid;
        this.LoadXml(url);
    },
    Request:
    function (url, onLoad, onError,
        sendData, dataType, requestType, callBackGuid) {
            new Ajax.LoadContent(url, onLoad, onError, sendData, dataType, requestType, callBackGuid);
    },
    ProcSubmit:
    function (url, fromId, onCallBack, onError, callBackGuid) {
        var obj = $(fromId);
        var str = "";
        for (i = 0; i < obj.elements.length; i++) {
            e = obj.elements[i];
            if (e.type == "text"
            || e.type == "hidden"
            || e.type == "textarea"
            || e.type == "password") {
                str += "&" + e.name + "=" + escape(e.value);
            } else if (e.type == "radio") {
                if (e.checked) {
                    str += "&" + e.name + "=" + escape(e.value);
                }
            } else if (e.type == "select-one") {
                str += "&" + e.id + "Id=" + escape(e.options[e.selectedIndex].value);
                str += "&" + e.id + "Name=" + escape(e.options[e.selectedIndex].text);
            }
        }
        str = str.replace(str.substring(0, 1), "");
        Ajax.Request(url, onCallBack, onError, str, 'text', null, callBackGuid);
    }
}

//
//自定义内容属性
//
Ajax.LoadContent.prototype = {
    LoadXml: function (url) {
        if (window.ActiveXObject) {
            for (var i in this.XmlHttpType) {
                try {
                    this.req = new ActiveXObject(this.XmlHttpType[i]); break;
                } catch (e) {
                    continue;
                }
            }
        } else if (window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        }
        if (this.req) {
            var tempLoad = this;
            this.req.overrideMimeType ? this.req.overrideMimeType("text/xml") : null;
            this.req.onreadystatechange = function () {
                tempLoad.ReadyStateChange.call(tempLoad);
            }
            if (this.sendData) {
                this.req.open("POST", this.url, true);
                if (this.requestType && this.requestType.toLowerCase() == "xml") {
                    this.req.setRequestHeader("Content-Type", "text/xml; charset=gb2312");
                }
                else {
                    this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }
            } else {
                this.req.open("GET", this.url + '&xmlRequest=cmd', true); //temp add cmd
            }
            if (this.requestType
            && this.requestType.toLowerCase() == "xml") {
                this.req.send(this.sendData);
            }
            else {
                this.req.send(this.sendData);
            }
        }
    },
    XmlHttpType: {
        msxml2: "MSXML2.XMLHTTP",
        msxml2_5: "MSXML2.XMLHTTP.5.0",
        msxml2_3: "MSXML2.XMLHTTP.3.0",
        msxml2_4: "MSXML2.XMLHTTP.4.0",
        micxmlhttp: "Microsoft.XMLHTTP"
    },
    ReadyStateChange: function () {
        if (this.req.readyState == 4) {
            if (this.req.status == 200 || this.req.status == 0) {
                if (this.dataType.toLowerCase() == "xml") {
                } else {
                }
                this.onload.call(this, this.callBackGuid);
            } else {
                this.onerror.call(this);
            }
        }
    },
    GetLoadData: function () {
        return this.data;
    },
    defaultError: function () {
        alert('error .. ResponseHeads=' + this.req.getAllResponseHeaders());
    }
}
Ajax.pro = function () { alert('f'); }